import Client from './client';
import * as SRTUploader from '@yanxiu/uploader'
import {computeMd5} from '../utils/helper';

export default class SRT extends Client {
    run(callback) {
        const {file, id, config} = this;
        const {onProgress, onSuccess, onError} = callback;

        const key = computeMd5(file);
        const observable = SRTUploader.upload(file, key, config);

        const observer = {
            complete: (data) => onSuccess({id, data: {...data, host: config.host, pType: 'srt'}}),
            error: (info) => onError({id, info}),
            progress: (progress) => onProgress({id, progress: {percent: progress.total.percent}}),
        };

        this.subscription = observable.subscribe(observer);
    }

    abort() {
        if (this.subscription) {
            this.subscription.unsubscribe();
        }
    }
}
